/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.dei.perla.parser.expressions;

/**
 * Costante di tipo ID
 * @author Stefano Vettor
 */
public class ConstantID extends ConstantBuiltin {
	//Valore della costante
	private String idValue;
	
	/**
	 * Crea una costante impostata al valore di default
	 */
	public ConstantID() {
		this.initialize("");
	}
	
	/**
	 *  Crea una costante impostata al valore specificato
	 */
	public ConstantID(String parStringValue) {
		this.initialize(parStringValue);
	}
	
	public ConstantID(Integer parIntegerValue) {
		this.initialize(parIntegerValue.toString());
	}

	/**
	 * Inizializza la costante	
	 */
	private void initialize(String parValue) {
		this.idValue = parValue;
	}
	
	/**
	 * Casta il valore della costante a ID
	 */
	public String getValueID() {
		return this.idValue;
	}
	
	public int getValueInt(){
		return (new Integer(this.idValue)).intValue();
	}
	
	public String getValueString() {
		return new String(idValue);
	}
	
	/**
	 * Il metodo ritorna un oggetto di tipo ConstantLogic rispecchiante nel valore 
	 * l'esito del confronto di uguaglianza.
	 */
	public ConstantLogic isEqual(Constant parConst) throws OperationNotSupportedException, ConstantCastException {
		
		if (parConst.getClass() == ConstantID.class) {
			if (idValue.equals(parConst.getValueID()) ) 
				return new ConstantLogic(LogicValue.TRUE);
			else
				return new ConstantLogic(LogicValue.FALSE);
		}
		else if (parConst.getClass() == ConstantInteger.class) {
			if (idValue.equals(parConst.toString())) 
				return new ConstantLogic(LogicValue.TRUE);
			else
				return new ConstantLogic(LogicValue.FALSE);
		}
		else if (parConst.isUserDefined())
			return parConst.isEqual(this);
		else if (parConst.getClass() == ConstantNull.class)
			return new ConstantLogic(LogicValue.UNKNOWN);
		else
			throw new ConstantCastException("Impossible to cast "+ parConst.getClass().toString() +" to ConstantID to compare it.");
	}
	
	/**
	 * Il metodo ritorna un oggetto di tipo ConstantLogic rispecchiante nel valore 
	 * l'esito del confronto di disuguaglianza.
	 */
	// TODO: ma non sarebbe meglio richiamare isEqual e poi negarne il risultato?
	public ConstantLogic isUnequal(Constant parConst) throws OperationNotSupportedException, ConstantCastException {
		if (parConst.getClass() == ConstantID.class) {
			if (idValue.equals(parConst.getValueID()) ) 
				return new ConstantLogic(LogicValue.FALSE);
			else
				return new ConstantLogic(LogicValue.TRUE);
		}
		else if (parConst.getClass() == ConstantInteger.class) {
			if (idValue.equals(parConst.toString())) 
				return new ConstantLogic(LogicValue.FALSE);
			else
				return new ConstantLogic(LogicValue.TRUE);
		}
		else if (parConst.isUserDefined())
			return parConst.isUnequal(this);
		else if (parConst.getClass() == ConstantNull.class)
			return new ConstantLogic(LogicValue.UNKNOWN);
		else
			throw new ConstantCastException("Impossible to cast parConst to ConstantID to compare it.");
	}
	
	/**
	 * Restituisce una stringa rappresentante il valore della Costante
	 */
	public String toString() {
		return this.idValue;
	}
}
